/************************************************************************************
 * @File name   :      DealerMacQueryController.java
 *
 * @Author      :      Wu Bin
 *
 * @Date        :      2011-6-17
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------------
 * Date                          Who              Version               Comments
 * 2011-6-17 ä¸Šå�ˆ10:15:22             Wu Bin          1.0             Initial Version
 * 2011-10-19 ä¸Šå�ˆ8:47:01             Wu Jianxi       1.1             refactor code
 * 2011-10-26 ä¸‹å�ˆ05:04:07            Wu Jianxi       1.2             fix bug
 * 2011-11-1 ä¸‹å�ˆ09:01:55             Wu Jianxi       1.3             fix bug
 ************************************************************************************/
package com.cap.bts.framework.common.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.cap.bts.framework.common.dto.DealerQueryDto;
import com.cap.bts.framework.common.dto.DealerQueryResultDto;
import com.cap.bts.framework.common.dto.MacQueryDto;
import com.cap.bts.framework.common.dto.MacQueryResultDto;
import com.cap.bts.framework.common.service.IDealerMacService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cap.bts.commonfunc.ge.dealer.service.IDealerService;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.usermgnt.constant.OrganizationType;
import com.cap.bts.framework.usermgnt.domain.Organization;
import com.cap.bts.framework.usermgnt.domain.User;
import com.cap.bts.framework.usermgnt.service.IOrganizationMgntService;

/**
 *
 */
@Controller
@RequestMapping(value = "/common")
public class DealerMacQueryController extends BaseController {
    
    @Autowired
    private IDealerMacService dealerMacService;
    
    @Resource(name = "dealerService")
    private IDealerService dealerService;
    
    @Autowired
    private IOrganizationMgntService orgMgntService;
    
    /**
     * ç»�é”€å•†æ��ç¤ºé¦–é¡µï¼ˆæŸ¥è¯¢å’Œåˆ†é¡µï¼‰
     * @Date        :      2011-6-17
     * @param dealerQueryDto DealerQueryDto
     * @param model Model
     * @return url String
     * @throws AppException all appException
     */
    @RequestMapping(value = "/list")
    public String queryDealerWithPaging(DealerQueryDto dealerQueryDto,
        Model model, HttpServletRequest request) throws AppException {
        
        User user = this.getCurrentUser();
        String inputName = removeNull((String)request.getParameter("inputName"));
        String dealerAbbrName = removeNull((String)request.getParameter("dealerAbbrName"));
        List<Organization> orgList = orgMgntService.getOrgByOrgType(OrganizationType.RFS, user.getBrandId());
        
        dealerQueryDto.setBrandId(user.getBrandId());
      
        model.addAttribute("queryDto", dealerQueryDto);
        model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dealerQueryDto.getPager());
        model.addAttribute("orgList", orgList);
        // model.addAttribute("isFlag", dealerQueryDto.getIsFlag());
        
        if ("".equals(inputName)) {
            inputName = "imac";
        }            
        if ("".equals(dealerAbbrName)) {
            dealerAbbrName = "dealerAbbrName";
        }            
        
        model.addAttribute("inputName", inputName);
        model.addAttribute("dealerAbbrName", dealerAbbrName);
        
        
        return "common/dealer_query";
    }
    
    /**
     * 
     * @Date        :      2011-11-1
     * @param excludeDealerCode
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/dta/validate/destdealercode")
    @ResponseBody
    public boolean validateDealerTransAppDealerCode(@RequestParam("excludeDealerCode") String excludeDealerCode, @RequestParam("dealerCode") String dealerCode) throws AppException{
        User user = getCurrentUser();
        
        return dealerService.validateDealerTransAppDestDealerCode(user.getBrandId(), excludeDealerCode, dealerCode);
    }
    
    /**
     * ç§»é™¤Null
     * 
     * @Date : 2011-6-15
     * @param str
     *            String
     * @return String
     */
    private String removeNull(String str) {
        if (str == null) {
            return "";
        } else {
            return str;

        }
    }
}
